﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Time
{
    class Time
    {
        private int hours;
        private int minutes;

        public int Hours
        {
            get { return hours; }
            set 
            {
                hours = value;
                while (hours<0)
                {
                    hours += 24;
                }
                hours %= 24;
            }
        }

        public int Minutes
        {
            get { return minutes; }
            set 
            {
                minutes = value;
                while (minutes > 59)
                {
                    minutes %= 60;
                    hours++;
                }

                while (minutes < 0)
                {
                    minutes += 60;
                    hours--;
                }
            }
        }

        public Time()
            :this(0,0)
        {
        }

        public Time(int hours)
            :this(hours,0)
        {
        }

        public Time(int hours, int minutes)
        {
            this.Hours = hours;
            this.Minutes = minutes;
        }

        public override string ToString()
        {
            string result = string.Format("The time is {0}:{1:D2}", hours, minutes);
            return result;
        }

        public static Time operator +(Time first, int moreMinutes)
        {
            int minutes = first.Minutes + moreMinutes;
            int hours = first.Hours;
            while (minutes > 59)
            {
                minutes %= 60;
                hours++;
            }
            hours %= 24;
            return new Time(hours, minutes);
        }

        public static Time operator -(Time first, int lessMinutes)
        {
            int minutes = first.Minutes - lessMinutes;
            int hours = first.Hours;
            while (minutes < 0)
            {
                minutes += 60;
                hours--;
            }
            while (hours < 0)
            {
                hours += 24;
            }
            return new Time(hours, minutes);
        }

        public static Time operator +(Time first, Time second)
        {
            int minutes = first.Minutes + second.Minutes;
            int hours = first.Hours + second.Hours;
            while (minutes > 59)
            {
                minutes %= 60;
                hours++;
            }
            hours %= 24;
            return new Time(hours, minutes);
        }

        public static Time operator -(Time first, Time second)
        {
            int minutes = first.Minutes - second.Minutes;
            int hours = first.Hours - second.Hours;
            while (minutes < 0)
            {
                minutes += 60;
                hours--;
            }
            while (hours<0)
            {
                hours += 24;
            }
            return new Time(hours, minutes);
        }

        public static Time operator ++(Time first)
        {
            int minutes = first.Minutes + 1;
            int hours = first.Hours;
            while (minutes > 59)
            {
                minutes %= 60;
                hours++;
            }
            hours %= 24;
            return new Time(hours, minutes);
        }

        public static Time operator --(Time first)
        {
            int minutes = first.Minutes - 1;
            int hours = first.Hours;
            while (minutes < 0)
            {
                minutes += 60;
                hours--;
            }
            while (hours < 0)
            {
                hours += 24;
            }
            return new Time(hours, minutes);
        }
    }
}
